package com.dulichdanang.location.location;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import lib.map.MyMedia;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.dulichdanang.location.locationCat.LocationCat;

import android.graphics.drawable.Drawable;
import android.net.ParseException;
import android.util.Log;

public class Location {
	// id, tên địa điểm, từ khóa tìm kiếm google map, chuỗi lưu trữ kinh độ, vĩ độ - "13.333,32.333"
	public int id;
	public int catID;
	public String name;
	public String keywork;
	public String ordinate;
	public String address;
	public String detail;
	public Drawable image;
	public String share;
	public Location getDetaill(String id){
		JSONArray jArray;
		String result = null;
		InputStream is = null;
		StringBuilder sb = null;
		Location lo = new Location();
		//Phần này tạo kết nối
		try{
		     HttpClient httpclient = new DefaultHttpClient();
		     HttpPost httppost = new HttpPost("http://10.0.2.2:8080/webservice/index.php/location/getdetail/"+id);
		     HttpResponse response = httpclient.execute(httppost);
		     HttpEntity entity = response.getEntity();
		     is = entity.getContent();
		     }catch(Exception e){
		         Log.e("log_tag", "Error in http connection"+e.toString());
		    }
		
		// Đọc dữ liệu về dưới dạng chuỗi
		try{
		      BufferedReader reader = new BufferedReader(new InputStreamReader(is,"utf-8"),8);
		       sb = new StringBuilder();
		       sb.append(reader.readLine() + "\n");

		       String line="0";
		       while ((line = reader.readLine()) != null) {
		                      sb.append(line + "\n");
		        }
		        is.close();
		        result=sb.toString();
		        }catch(Exception e){
		              Log.e("log_tag", "Error converting result "+e.toString());
		   }
		//paring data 
		try{
		      jArray = new JSONArray(result);
		      JSONObject json_data= jArray.getJSONObject(0);
		      lo.id = json_data.getInt("id");
		      lo.name = json_data.getString("name");
		      lo.keywork = json_data.getString("keywork");
		      lo.ordinate = json_data.getString("ordinate");
		      lo.address = json_data.getString("address");
		      lo.image = image = MyMedia.LoadImageFromWebOperations(json_data.getString("image"),"image");
		      lo.detail = json_data.getString("detail");
		      lo.share = json_data.getString("share");
		      return lo;
		      }
		      catch(JSONException e1){
		    	  e1.printStackTrace();
		      } catch (ParseException e1) {
		    	 e1.printStackTrace();
		      }
		return null;
	}
}
